/**
 * 
 */
package travibot.ui.gui.forms;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;

import travibot.engine.GameSessionFacade;
import travibot.engine.MaterialFactory;
import travibot.model.Cost;
import travibot.model.Field;
import travibot.model.Structure;
import travibot.model.materials.Clay;
import travibot.model.materials.Crop;
import travibot.model.materials.Iron;
import travibot.model.materials.Wood;
import travibot.ui.gui.forms.viewers.FieldsViewer;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Button;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * @author tajzivit
 *
 */
public class Fields extends Composite implements CompositeForm {

    private FieldsViewer viewer = null;
    private Tree fieldsTree;
    private MainForm mainForm = null;  //  @jve:decl-index=0:
    private Text tbName = null;
    private Label lblName = null;
    private Label lblLevel = null;
    private Text tbLevel = null;
    private Label lblNetLevelCosts = null;
    private Group grpMaterials = null;
    private Label lblClay = null;
    private Label lblWood = null;
    private ImageRegistry imageRegistry;  //  @jve:decl-index=0:
    private Label lblIron = null;
    private Label lblCrop = null;
    private Button btnUpgrade = null;

    public Fields(Composite parent, int style) {

        super(parent, style);
        initialize();
    }

    private void initialize() {

        GridData gridData12 = new GridData();
        gridData12.horizontalAlignment = GridData.BEGINNING;
        gridData12.horizontalSpan = 2;
        gridData12.verticalAlignment = GridData.BEGINNING;
        GridData gridData11 = new GridData();
        gridData11.horizontalAlignment = GridData.BEGINNING;
        gridData11.horizontalSpan = 2;
        gridData11.verticalAlignment = GridData.BEGINNING;
        GridData gridData3 = new GridData();
        gridData3.horizontalAlignment = GridData.FILL;
        gridData3.grabExcessHorizontalSpace = true;
        gridData3.verticalAlignment = GridData.BEGINNING;
        GridData gridData2 = new GridData();
        gridData2.horizontalAlignment = GridData.BEGINNING;
        gridData2.verticalAlignment = GridData.BEGINNING;
        GridData gridData1 = new GridData();
        gridData1.horizontalAlignment = GridData.BEGINNING;
        gridData1.verticalAlignment = GridData.BEGINNING;
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.verticalAlignment = GridData.BEGINNING;
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 4;
        this.setLayout(gridLayout);
        setSize(new Point(496, 200));
        {
            GridData fieldsTreeLData = new GridData();
            fieldsTreeLData.verticalAlignment = GridData.FILL;
            fieldsTreeLData.grabExcessVerticalSpace = true;
            fieldsTreeLData.horizontalSpan = 2;
            fieldsTreeLData.verticalSpan = 5;
            fieldsTreeLData.widthHint = 150;
            fieldsTree = new Tree(this, SWT.BORDER);
            fieldsTree.setLayoutData(fieldsTreeLData);
            lblName = new Label(this, SWT.NONE);
            lblName.setText("Name");
            lblName.setLayoutData(gridData2);
            tbName = new Text(this, SWT.BORDER);
            tbName.setLayoutData(gridData3);            
            lblLevel = new Label(this, SWT.NONE);
            lblLevel.setText("Level");
            lblLevel.setLayoutData(gridData1);
            tbLevel = new Text(this, SWT.BORDER);
            tbLevel.setLayoutData(gridData);
            lblNetLevelCosts = new Label(this, SWT.NONE);
            lblNetLevelCosts.setText("Next level costs");
            lblNetLevelCosts.setLayoutData(gridData11);
            createGrpMaterials();
            btnUpgrade = new Button(this, SWT.NONE);
            btnUpgrade.setText("Upgrade");
            btnUpgrade.setLayoutData(gridData12);
            viewer = new FieldsViewer(fieldsTree);
        }
    }

    public void setInput(Object object){
        
        if(viewer != null)
            viewer.setInput(object);
    }

    public void setMainForm(MainForm mainform) {

        this.mainForm = mainform;
    }
    
    public void updateDetails(Structure structure){
        if(structure instanceof Field){
            Field f = (Field)structure;
            tbName.setText(f.getName());
            tbLevel.setText(String.valueOf(f.getLevel().getValue()));
            Cost cost = f.getNextLevelCost();
            if(cost != null){
                GameSessionFacade game = mainForm.getGameSession();
                MaterialFactory fact = game.getMetabaseManager().getMaterialFactory();            
                int clayCost = cost.getCost(fact.getMaterial(Clay.class));
                int woodCost = cost.getCost(fact.getMaterial(Wood.class));
                int irondCost = cost.getCost(fact.getMaterial(Iron.class));
                int cropCost = cost.getCost(fact.getMaterial(Crop.class));
                if(imageRegistry != null){
                    lblClay.setImage(imageRegistry.get("image.material.clay"));
                    lblWood.setImage(imageRegistry.get("image.material.wood"));
                    lblIron.setImage(imageRegistry.get("image.material.iron"));
                    lblCrop.setImage(imageRegistry.get("image.material.crop"));
                }
                lblClay.setText(String.valueOf(clayCost));
                lblWood.setText(String.valueOf(woodCost));
                lblIron.setText(String.valueOf(irondCost));
                lblCrop.setText(String.valueOf(cropCost));
                if(imageRegistry != null){
                    lblClay.setImage(imageRegistry.get("image.material.clay"));
                    lblWood.setImage(imageRegistry.get("image.material.wood"));
                    lblIron.setImage(imageRegistry.get("image.material.iron"));
                    lblCrop.setImage(imageRegistry.get("image.material.crop"));
                }
            } else {
                lblClay.setText("");
                lblWood.setText("");
                lblIron.setText("");
                lblCrop.setText(""); 
            }
        }        
    }
    
   

    /**
     * This method initializes grpMaterials	
     *
     */
    private void createGrpMaterials() {
    
        GridData gridData8 = new GridData();
        gridData8.grabExcessHorizontalSpace = true;
        gridData8.verticalAlignment = GridData.CENTER;
        gridData8.horizontalAlignment = GridData.FILL;
        GridData gridData7 = new GridData();
        gridData7.grabExcessHorizontalSpace = true;
        gridData7.verticalAlignment = GridData.CENTER;
        gridData7.horizontalAlignment = GridData.FILL;
        GridData gridData6 = new GridData();
        gridData6.horizontalAlignment = GridData.FILL;
        gridData6.grabExcessHorizontalSpace = true;
        gridData6.verticalAlignment = GridData.CENTER;
        GridData gridData5 = new GridData();
        gridData5.horizontalAlignment = GridData.FILL;
        gridData5.grabExcessHorizontalSpace = true;
        gridData5.verticalAlignment = GridData.CENTER;
        GridLayout gridLayout1 = new GridLayout();
        gridLayout1.numColumns = 4;
        GridData gridData4 = new GridData();
        gridData4.horizontalSpan = 2;
        gridData4.verticalAlignment = GridData.BEGINNING;
        gridData4.grabExcessHorizontalSpace = true;
        gridData4.horizontalAlignment = GridData.FILL;
        grpMaterials = new Group(this, SWT.NONE);
        grpMaterials.setLayoutData(gridData4);
        grpMaterials.setLayout(gridLayout1);
        lblClay = new Label(grpMaterials, SWT.NONE);
        lblClay.setText("Label");
        lblClay.setLayoutData(gridData5);
        lblWood = new Label(grpMaterials, SWT.NONE);
        lblWood.setText("Label");
        lblWood.setLayoutData(gridData6);
        lblIron = new Label(grpMaterials, SWT.NONE);
        lblIron.setText("Label");
        lblIron.setLayoutData(gridData7);
        lblCrop = new Label(grpMaterials, SWT.NONE);
        lblCrop.setText("Label");
        lblCrop.setLayoutData(gridData8);
        
    }

    public void setImageRegistry(ImageRegistry imageRegistry) {

        this.imageRegistry = imageRegistry;        
    }
    
    
}  //  @jve:decl-index=0:visual-constraint="2,11"
